package cn.abyss.admin.ums.business;

import cn.abyss.admin.consts.AdminWebConstants;
import cn.abyss.admin.enums.AdminWebExceptionEnum;
import cn.abyss.admin.exception.AdminWebException;
import cn.abyss.admin.exception.BaseException;
import cn.abyss.admin.ums.model.UmsAdminRoleRelation;
import cn.abyss.admin.ums.model.UmsResource;
import cn.abyss.admin.ums.service.UmsAdminRoleRelationService;
import cn.abyss.admin.util.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 *
 * @author zhangsj
 * @date Created on 2021/8/26
 */
@Component
public class UmsAdminRoleRelationBusiness {
    @Autowired
    private UmsAdminRoleRelationService umsAdminRoleRelationService;


    /**
     * 获取用户下所有资源
     * @param adminId
     * @return
     */
    public List<UmsResource> getResourceList(Long adminId) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("adminId", adminId);
            List<UmsResource> umsResources = umsAdminRoleRelationService.queryResourceList(map);
            return umsResources;
        } catch (BaseException bex) {
            throw bex;
        } catch (Exception e) {
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_RESOURCE_QUERY_BYNAME_FAIL, e);
        }
    }

    /**
     * 根据角色Id获取用户角色关联
     * @param roleId
     * @return
     */
    public List<UmsAdminRoleRelation> getResourceListByRoleId(Long roleId) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("roleId", roleId);
            List<UmsAdminRoleRelation> umsAdminRoleRelations = umsAdminRoleRelationService.query(map);
            return umsAdminRoleRelations;
        } catch (BaseException bex) {
            throw bex;
        } catch (Exception e) {
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_MEMBER_ROLE_RELATION_QUERY_FAIL, e);
        }
    }

    public List<UmsAdminRoleRelation> getResourceListByRoleIds(List<Long> roleIds) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("roleIdList", roleIds);
            List<UmsAdminRoleRelation> umsAdminRoleRelations = umsAdminRoleRelationService.query(map);
            return umsAdminRoleRelations;
        } catch (BaseException bex) {
            throw bex;
        } catch (Exception e) {
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_MEMBER_ROLE_RELATION_QUERY_FAIL, e);
        }
    }

    public List<Long> getAdminIdListByResourceId(Long resourceId) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("resourceId", resourceId);
            List<ResultMap> umsInfoList = umsAdminRoleRelationService.listForUmsInfo(map);
            List<Long> adminIdList = umsInfoList.stream().map(umsInfo -> (Long)umsInfo.get("adminId")).collect(Collectors.toList());
            return adminIdList;
        } catch (BaseException bex) {
            throw bex;
        } catch (Exception e) {
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_MEMBER_ROLE_RELATION_QUERY_FAIL, e);
        }

    }
}
